package com.zw.cc.stock.service;

import com.zw.cc.stock.cache.CacheCtrl;
import com.zw.cc.stock.cache.SelectCache;
import com.zw.cc.stock.common.PolicyTypeEnum;
import com.zw.cc.stock.exception.BusinessException;
import com.zw.cc.stock.model.QuotesPo;
import com.zw.cc.stock.model.StocksPo;
import com.zw.cc.stock.model.vo.StocksVo;
import com.zw.cc.stock.service.policy.CalculateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by zhaowuz on 2018/1/10
 */
@Service
public class PolicyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PolicyService.class);

    @Autowired
    private CacheCtrl cacheCtrl;

    @Autowired
    private QuotesService quotesService;


    private Map<String, CalculateService> calculateServiceMap;

    @Autowired
    public PolicyService(ApplicationContext context) {
        this.calculateServiceMap = context.getBeansOfType(CalculateService.class);
    }


    public List<StocksVo> getStocksVoList() {
        List<StocksVo> vo = new ArrayList<>();
        for(StocksPo stocksPo : cacheCtrl.getStocksPoList()) {
            StocksVo stocksVo = new StocksVo();
            BeanUtils.copyProperties(stocksPo,stocksVo);
            vo.add(stocksVo);
        }
        return vo;
    }

    public void calculate(String code){
        if (CollectionUtils.isEmpty(this.calculateServiceMap)) {
            throw new BusinessException("DispatchService.calculateServiceMap is null");
        }
        CalculateService calculateService;
        PolicyTypeEnum policyType;
        for (Map.Entry<String, CalculateService> entry : calculateServiceMap.entrySet()) {
            calculateService = entry.getValue();
            policyType = calculateService.getType();
            List<QuotesPo> quotesPos = cacheCtrl.getQuotesTable().get(code);
            if (CollectionUtils.isEmpty(quotesPos) || quotesPos.size()<policyType.getInterval()) {
                continue;
            }
            List<QuotesPo> list = new ArrayList<>(quotesPos);
            Collections.reverse(list);
            //现价 如果有今天行情了 就不在查询实时行情
            if (list.get(list.size() - 1).getTradeDate() <
                    Integer.valueOf(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")))) {
                QuotesPo quotesPo = quotesService.getPriceNow(code);
                if (null == quotesPo) {
                    continue;
                }
                list.add(quotesPo);
            }
            if (calculateService.calculate(list)) {
                LOGGER.info("计算结果：股票 {} 满足 {} 指标", code, policyType.getDesc());
                StocksVo stocksVo = new StocksVo();
                stocksVo.setCreateDate(new Date());
                BeanUtils.copyProperties(list.get(0),stocksVo);
                stocksVo.setPrice(list.get(0).getClose());
                SelectCache.STOCKSVO_TABLE.put(policyType.getCode(), code, stocksVo);
            }
        }
    }

}
